package deadQueues;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import utils.RabbitMQUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class Consumer1 {

    //普通交换机名称
    private static final String NORMAL_EXCHANGE = "normal_exchange";
    //死信交换机名称
    private static final String DEAD_EXCHANGE = "dead_exchange";

    public static void main(String[] args) throws IOException {
        Channel channel = RabbitMQUtils.getChannel();

        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);

        //申明死信队列
        String deadQueue = "dead-queue";
        channel.queueDeclare(deadQueue,false,false,false,null);
        //死信队列绑定：队列、交换机、路由键（routingKey）
        channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi");

        //正常队列绑定死信队列信息
        Map<String,Object> param = new HashMap<String,Object>();
        //正常队列设置死信交换机 参数 key 是固定值
        param.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //正常队列设置死信 routing-key 参数 key 是固定值
        param.put("x-dead-letter-routing-key", "lisi");

        //正常队列
        String normalQueue = "normal-queues";
        channel.queueDeclare(normalQueue,false,false,false,param);
        channel.queueBind(normalQueue,NORMAL_EXCHANGE,"zhangsan");

        System.out.println("等待接收消息。。。。。。");

        DeliverCallback deliverCallback = (consumer,deliver)->{
            String message = new String(deliver.getBody(), StandardCharsets.UTF_8);
            System.out.println("001接受到的消息："+message);
        };
        channel.basicConsume(normalQueue,true,deliverCallback,consumerTag -> {
        });
    }
}
